clear all

// SET MACROS
global Input  "Y:/limited/Michigan_CTE/funding_change/data_derived/imported"
global Output "Y:/limited/Michigan_CTE/funding_change/data_derived"

// LOAD DATA
use $Input/school_level.dta

// RENAME VARIABLE
rename bcode school
rename year cohort9
rename dcode district
rename entitytype entity_type
rename highgrade high_grade
rename per_frl prop_frl

// CLEAN VARIABLES


gen schoolchar_imp = inlist(.,charter,enroll,fte_experience,urbanicity,d_urban)
recode charter enroll fte_experience urbanicity (.=0)

// School type
bys school: gegen city  = max(urbanicity == 1)
bys school: gegen suburb  = max(urbanicity == 2)
replace suburb = 0 if city == 1
bys school: gegen town_rural  = max(urbanicity > 2)
replace town_rural = 0 if inlist(1,city,suburb)

gen trad_ps = school_type == 1

// KEEP VARIABLES
keep school cohort9 district entity_type school_type high_grade level charter enroll ///
  fte_experience city suburb town_rural trad_ps d_urban schoolchar_imp

// ORDER VARIABLES
order school cohort9 district school_type trad_ps entity_type high_grade level charter ///
  enroll fte_experience city suburb town_rural d_urban schoolchar_imp

// LABEL VARIABLES
label var school "School"
label var cohort9 "Ninth-grade cohort"
label var district "District"
label var trad_ps "Traditional public school"
label var charter "Charter school"
label var city "City school"
label var suburb "Suburb school"
label var town_rural "Town or Rural school"
label var schoolchar_imp "Imputed School Characteristics"


// SORT AND SAVE
sort school cohort9
compress
save $Output/school_level.dta, replace
